import {request} from "../../utils/util";

let app = getApp()
Page({
  data: {
    swiperIdx: 0,
    top: app.globalData.rightBarInfo.top,
    height: app.globalData.rightBarInfo.height,
    scrollHeight: '0',
    arrangeItemToggle: false,
    sportsTypeId: 0,
    details: {},
  },
  chooseSports (e) {
    this.setData({
      sportsTypeId: e.currentTarget.dataset.id
    })
  },
  // 获取详情数据
  getDetails() {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: `/api/web/venue/detail/${this.options.id}`,
      success: ({ code, data, message }) => {
        wx.hideLoading()
        if (code === 200) {
          data.open_time_show = data.open_time.substr(0, 5)
          data.close_time_show = data.close_time.substr(0, 5)
          data.date.forEach(item => {
            item.date_show = item.date.substr(5)
          })
          this.setData({
            details: data,
            sportsTypeId: data.sports[0].id
          })
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  toComments () {
    wx.navigateTo({
      url: `/pages/comments/index?venue_id=${this.options.id}`
    })
  },
  toggleArrangeItem () {
    this.setData({
      arrangeItemToggle: !this.data.arrangeItemToggle
    })
  },
  toReserve (ev) {
    if (!app.globalData.userInfo.nickname) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/mine/index'
        })
      }, 1000)
      return false
    } else if (!app.globalData.userInfo.telephone) {
      wx.showToast({
        title: '请绑定手机号',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 1000)
      return false
    }
    let { item } = ev.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/reserve/index?sport_id=${this.data.sportsTypeId}&venue_id=${this.options.id}&date=${item.date}&dayList=${JSON.stringify(this.data.details.date)}`
    })
  },
  bindChange (e) {
    this.setData({
      swiperIdx: e.detail.current
    })
  },
  // 打开地图
  viewLocation() {
    const {
      details: { longitude, latitude }
    } = this.data;
    try {
      wx.openLocation({
        latitude: parseFloat(latitude),
        longitude: parseFloat(longitude),
        scale: 14
      });
    } catch (e) {
      wx.showToast({ title: "定位失败！" });
    }
  },
  previewImg(ev) {
    let { item } = ev.currentTarget.dataset;
    wx.previewImage({
      current: item, // 当前显示图片的http链接
      urls: this.data.details.venue_img // 需要预览的图片http链接列表
    })
  },
  onLoad () {
    this.getDetails()
  },
})
